#!/bin/sh

# ELEVATION
alias doas='$ELEVATOR '
alias sudo='$ELEVATOR '
alias ssu='$ELEVATOR '
# END_ELEVATION


# KISS
alias kiss='$ELEVATOR kiss'
alias fixrepo='export idnum=$(id -u); 
               doas chown -R user:user $REPOS;
               unset idnum'

alias ka='kiss a'
alias kal='kiss a | $PAGER'
alias kag='kiss a | grep'
kagl () { kiss a | grep $@ | $PAGER ; }
kaga ()
{ 
  for i in $@; do
    kiss a | grep "$i" | kiss a -
  done
}

alias kb='kiss build'
alias ku='kiss update; kiss update'
alias kub='kiss update; kiss b'
alias kd='kiss download'

alias ks='kiss search'
ksl () { kiss s $@ | less ; }
cks () { cd $(kiss s $1); }
ksa ()
{
    pkg=''
    for i in `echo $KISS_PATH | sed 's/:/ /g'`; do
        pkg+="`ls -1 $i`"
    done
    echo "$pkg" | sort
    unset pkg i
}
alias ksal='ksa | less'

alias kr='kiss remove'

alias kh='kiss help'
alias khi='kiss h install'
alias khps='kiss h package-system'
alias khpm='kiss h package-manager'
khw  () { kiss h "wiki/$@"; }
khwx () { kiss h "wiki/xorg/$@"; }
khws () { kiss h "wiki/software/$@"; }

alias kl='kiss list'
alias klg='kiss l | grep'
alias kll='kiss l | $PAGER'

klb ()
{
	for i in $@; do
		kiss l $i || kiss b $i
	done
}

ksb ()
{
  for i in $@; do
    search="`kiss s $i`"
    test "$search" || return 1
    for j in $search; do
      listing="`echo $j | awk -F/ '{print $NF}'`"
      kiss l "$listing" || kiss b "$listing"
    done
  done
  unset i j search listing
}

alias kf="\kiss fork"
alias kn="\kiss new"
alias kc="\kiss checksum"

alias ko='kiss orphans'
alias kog='kiss o | grep'
alias kol='kiss o | $PAGER'
kogl () { kiss o | grep $@ | $PAGER ; }

alias krd='kiss revdepends'

alias rebuild=' 				\
	TO_INSTALL=`kiss l | cut -f1 -d" "`; 	\
	kiss u;	\
	kiss b $TO_INSTALL && \
	kiss i $TO_INSTALL; \
	unset TO_INSTALL
	'
alias boxcutter='\
	ls -l /usr/bin | \
	grep busybox | \
	awk {print\ \$9} | \
	xargs -n1 kiss s 2>/dev/null'

alias kln='kiss l | awk "END {print NR}"'
# END_KISS


# PROCESSES
alias reboot='doas reboot'
alias poweroff='doas poweroff'
alias restart='reboot'
alias shutdown='poweroff'
# END_PROCESSES


# CORE
alias cp='cp -iprv'
alias mv='mv -i'
#alias rm='bak -d $HOME/.trash'
#alias rst='back -rd $HOME/.trash -s'
# END_CORE


# NAVIGATION
alias ...='cd ../..'
alias ....='cd ../../..'
alias back='cd -'
alias cr='cd $REPOS'
cw () { cd $(dirname $(which $i)); }

alias lf='$FILER'
alias ls='exa'

# Handle flag differences between ls and exa.
if test "`alias ls`" = "ls='exa'"; then
	alias l='ls -lF'	
  	alias la='ls -la'
	alias lr='ls -T'
 	alias lar='ls -aT'
else 
	alias l='ls -lh'
  	alias la='ls -A'
	alias lr='l -R' 
  	alias lar='ls -lAR'
fi

# Flags shared by both ls and exa 
alias lc='ls -1'
alias ll='ls -l'
alias lll='l | $PAGER'
alias lrl='lr | $PAGER'
alias lcl='ls -1 | $PAGER'

alias ld='l  /dev/sd*'
alias lb='ll /usr/bin'
alias lbg='l /usr/bin | grep'
alias lbl='lb | $PAGER'
lbgl () { ls -lhA /usr/bin | grep $@ | less ; }
lw () { l `which $@` ; }
# END_NAVIGATION


# GREP
alias h='history'
alias hl='history | $PAGER'
alias hg='history | grep'
alias mg='mount | grep'
alias gp='grep'

gr () 
{ 
	regex="$1"
	shift 1
	find $@ -type f | \
	xargs grep $regex
}

eg () 
{
    # Grep through echoed string.
    regex=$1
    shift 1
    echo $@ | grep "$regex"
    unset regex
}
# END_GREP


# SED
alias tabex='sed -i "s/\t/    /"'
alias stripc="sed -i -e 's/^[[:blank:]]*//' \
                     -e 's/[[:blank:]]*$//' \
                     -e 's/#.*//'"

com () 
{
	for i in $@; do
		sed -i '/$i/,/END_$i/s/.*/# &'
	done
}

ucom ()
{
	for i in $@; do
		sed -i '/$i/,/END_$i/s/^# //'
	done
}
# END_SED


# SOURCING
if test $SHELL = /bin/zsh -o $SHELL = /usr/bin/zsh; then
  alias reprof='. $HOME/.zshrc 2>/dev/null || . $HOME/.profile'
else
  alias greprof='source /etc/profile'
  alias reprof='greprof; source $HOME/.profile'
fi

srcd () 
{
  for directory in $@; do
    for file in directory; do
      [ -d $file ] && \
      srcd $file   || \
      source $directory/$file
    done
  done
  unset file
  unset directory
}
# END_SOURCING


# ENV_ED
for i in /home/user/.profile.d/*.sh; do
  f=`echo $i | sed -n -e "s/\/home\/user\/.profile.d\///" -e 's/\.sh//p'`
  alias "vi$f"="\$EDITOR $i"
done
unset f
unset i
# END_ENV_ED


# # PERMISSIONS
# for i in `seq -w 0 777`; do
#   alias $i="chmod -R $i"
# done
# for i in `seq -w 0 7777`; do
#   alias $i="chmod -R $i"
# done
# unset i
# # END_PERMISSIONS


# VIEWING
alias vi='$ELEVATOR $EDITOR'
alias less='$PAGER'
alias more='$PAGER'
alias cat='bat -f'
batr () { find $@ -type f -exec bat {} + ; }
alias catr='batr'
# END_VIEWING


# TAR
alias tarx='tar -xvf'
alias tarc='tar -cvf'
alias tbz2x='tar -xvjf'
alias tbz2c='tar -cvjf'
alias tgzx='tar -xvzf'
alias tgzc='tar -cvzf'
alias txzx='tar -xvJf'
alias txzc='tar -cvJf'
# END_TAR


# WIFI
alias wpa_supplicant='doas wpa_supplicant'
alias wifi='wpa_supplicant -B -c /etc/wpa_supplicant/default.conf -i wlan0'
alias scanwifi='wpa_cli -i wlan0 scan; sleep 1; wpa_cli -i wlan0 scan_results'

newwifi () { 
    name=/etc/wpa_supplicant
    [ $# -lt 3 ] && name="$name/$1" || name="$name/$3" 
    [ -r $name ] && echo "$name already exists." >&2 && exit 1
    wpa_passphrase "$1" "$2" > "$name" 
}

rmwifi () { for i in $@; do rm /etc/wpa_supplicant/$i.conf; done; } 

lswifi () { 
    for i in /etc/wpa_supplicant/*; do
        echo $i | sed -n -e 's/^\/etc\/wpa_supplicant\///' -e 's/.conf//' -e 'p'
    done
}

setwifi () {
    [ -f "/etc/wpa_supplicant/$1.conf" ] || { echo "$1 wifi preset not found." >&2 ; exit 1; }
    cp "/etc/wpa_supplicant/$1.conf" /etc/wpa_supplicant/default.conf
}
# END_WIFI


# GIT
alias gitc='git -C $REPOS clone'
alias giti='git init'
gitm () { git commit -m "$@"; }
alias gita='git add'
# END_GIT
